package clientView;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import object.Department;
import utility.StringUtil;
import clientMain.Main;
import clientUtilGUI.KeyValue;
import clientUtilGUI.UtilityGUI;
import database.Paging;


public class Dep_Add extends JScrollPane implements ActionListener{

	private JPanel panel;
	private JPanel panelHeader;
	private JPanel panelBody;
	private JTextField txtDepName;
	private JTextField txtDepAdd;
	private JTextField txtDepPhone;
	private JTextField txtTotalEm;
	private JButton btnSave;
	private JButton btnCancel;
	private JLabel lblTotalEm;
	private JLabel lblDepPhone;
	private JComboBox cbbDistrict;
	private JLabel lblDistrict;
	private JComboBox cbbProvince;
	private JLabel lblProvince;
	private JLabel lblDepAdd;
	private JTextArea txaDepDes;
	private JLabel lblDepDes;
	private JLabel lblDepName;
	private JScrollPane scrollPane;
	private JLabel lbl;
	private GridBagConstraints gbc_lbl;
	private GridBagConstraints gbc_lbl_1;
	private JPanel panelBtn;
	private GridBagConstraints gbc_cbbDistrict;
	private int districtId;
	private int provinceId;
	private Vector<JTextField> groupTxt = new Vector<JTextField>();
	private JLabel lblNote;
	

	/**
	 * Create the panel.
	 */
	public Dep_Add() {
		panel = new JPanel();
		
		setViewportView(panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0};
		gbl_panel.rowHeights = new int[]{0, 01};
		gbl_panel.columnWeights = new double[]{1.0};
		gbl_panel.rowWeights = new double[]{0.0, 1.0};
		panel.setLayout(gbl_panel);
		
		panelHeader = UtilityGUI.headerForm("Thêm Phòng Ban");
		GridBagConstraints gbc_panelHeader = new GridBagConstraints();
		gbc_panelHeader.insets = new Insets(8, 10, 10, 10);
		gbc_panelHeader.fill = GridBagConstraints.BOTH;
		gbc_panelHeader.gridx = 0;
		gbc_panelHeader.gridy = 0;
		panel.add(panelHeader, gbc_panelHeader);
		
		panelBody = new JPanel();
		GridBagConstraints gbc_panelBody = new GridBagConstraints();
		gbc_panelBody.insets = new Insets(5, 10, 10, 10);
		gbc_panelBody.fill = GridBagConstraints.BOTH;
		gbc_panelBody.gridx = 0;
		gbc_panelBody.gridy = 1;
		panel.add(panelBody, gbc_panelBody);
		GridBagLayout gbl_panelBody = new GridBagLayout();
		gbl_panelBody.columnWidths = new int[]{0, 0, 0};
		gbl_panelBody.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_panelBody.columnWeights = new double[]{1.0, 0.0, 1.0};
		gbl_panelBody.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
		panelBody.setLayout(gbl_panelBody);
		
		lblDepName = new JLabel("Tên Phòng Ban :");
		lblDepName.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblDepName = new GridBagConstraints();
		gbc_lblDepName.anchor = GridBagConstraints.EAST;
		gbc_lblDepName.insets = new Insets(3, 5, 5, 5);
		gbc_lblDepName.gridx = 0;
		gbc_lblDepName.gridy = 0;
		panelBody.add(lblDepName, gbc_lblDepName);
		
		txtDepName = new JTextField();
		txtDepName.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtDepName.setText(StringUtil.removeUnwantedSpace(txtDepName.getText()));
			}
		});
		GridBagConstraints gbc_txtDepName = new GridBagConstraints();
		gbc_txtDepName.fill = GridBagConstraints.BOTH;
		gbc_txtDepName.insets = new Insets(3, 3, 5, 5);
		gbc_txtDepName.gridx = 1;
		gbc_txtDepName.gridy = 0;
		panelBody.add(txtDepName, gbc_txtDepName);
		
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(UtilityGUI.colorNote);
		gbc_lbl = new GridBagConstraints();
		gbc_lbl.anchor = GridBagConstraints.WEST;
		gbc_lbl.insets = new Insets(3, 0, 5, 5);
		gbc_lbl.gridx = 2;
		gbc_lbl.gridy = 0;
		panelBody.add(lbl, gbc_lbl);
		
		lblDepDes = new JLabel("Mô Tả Nhiệm Vụ :");
		lblDepDes.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblDepDes = new GridBagConstraints();
		gbc_lblDepDes.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblDepDes.insets = new Insets(3, 5, 5, 5);
		gbc_lblDepDes.gridx = 0;
		gbc_lblDepDes.gridy = 1;
		panelBody.add(lblDepDes, gbc_lblDepDes);
		
		txaDepDes = new JTextArea();
		txaDepDes.setColumns(30);
		txaDepDes.setRows(5);
		txaDepDes.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txaDepDes.setText(StringUtil.removeUnwantedSpace(txaDepDes.getText()));
			}
		});
		scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.insets = new Insets(3, 3, 5, 5);
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 1;
		panelBody.add(scrollPane, gbc_scrollPane);
		scrollPane.setViewportView(txaDepDes);
		
		lblDepAdd = new JLabel("Địa Chỉ :");
		lblDepAdd.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblDepAdd = new GridBagConstraints();
		gbc_lblDepAdd.anchor = GridBagConstraints.EAST;
		gbc_lblDepAdd.insets = new Insets(3, 5, 5, 5);
		gbc_lblDepAdd.gridx = 0;
		gbc_lblDepAdd.gridy = 2;
		panelBody.add(lblDepAdd, gbc_lblDepAdd);
		
		txtDepAdd = new JTextField();
		txtDepAdd.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtDepAdd.setText(StringUtil.removeUnwantedSpace(txtDepAdd.getText()));
			}
		});
		GridBagConstraints gbc_txtDepAdd = new GridBagConstraints();
		gbc_txtDepAdd.fill = GridBagConstraints.BOTH;
		gbc_txtDepAdd.insets = new Insets(3, 3, 5, 5);
		gbc_txtDepAdd.gridx = 1;
		gbc_txtDepAdd.gridy = 2;
		panelBody.add(txtDepAdd, gbc_txtDepAdd);
		
		lblProvince = new JLabel("Tỉnh Thành :");
		lblProvince.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblProvince = new GridBagConstraints();
		gbc_lblProvince.anchor = GridBagConstraints.EAST;
		gbc_lblProvince.insets = new Insets(3, 5, 5, 5);
		gbc_lblProvince.gridx = 0;
		gbc_lblProvince.gridy = 3;
		panelBody.add(lblProvince, gbc_lblProvince);
		
		cbbProvince = UtilityGUI.getComboBox("Province", 0,"");
		cbbProvince.addActionListener(this);
		GridBagConstraints gbc_cbbProvince = new GridBagConstraints();
		gbc_cbbProvince.fill = GridBagConstraints.BOTH;
		gbc_cbbProvince.insets = new Insets(3, 3, 5, 5);
		gbc_cbbProvince.gridx = 1;
		gbc_cbbProvince.gridy = 3;
		panelBody.add(cbbProvince, gbc_cbbProvince);
		
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(UtilityGUI.colorNote);
		gbc_lbl_1 = new GridBagConstraints();
		gbc_lbl_1.anchor = GridBagConstraints.WEST;
		gbc_lbl_1.insets = new Insets(3, 0, 5, 5);
		gbc_lbl_1.gridx = 2;
		gbc_lbl_1.gridy = 3;
		panelBody.add(lbl, gbc_lbl_1);
		
		lblDistrict = new JLabel("Quận Huyện :");
		lblDistrict.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblDistrict = new GridBagConstraints();
		gbc_lblDistrict.anchor = GridBagConstraints.EAST;
		gbc_lblDistrict.insets = new Insets(3, 5, 5, 5);
		gbc_lblDistrict.gridx = 0;
		gbc_lblDistrict.gridy = 4;
		panelBody.add(lblDistrict, gbc_lblDistrict);
		
		cbbDistrict = new JComboBox();
		cbbDistrict.addItem("-- Chọn Quận(huyện) --");
		cbbDistrict.setFont(UtilityGUI.fontComboBox);
		cbbDistrict.setEnabled(false);
		gbc_cbbDistrict = new GridBagConstraints();
		gbc_cbbDistrict.fill = GridBagConstraints.BOTH;
		gbc_cbbDistrict.anchor = GridBagConstraints.CENTER;
		gbc_cbbDistrict.insets = new Insets(3, 3, 5, 5);
		gbc_cbbDistrict.gridx = 1;
		gbc_cbbDistrict.gridy = 4;
		panelBody.add(cbbDistrict, gbc_cbbDistrict);
		
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(UtilityGUI.colorNote);
		gbc_lbl = new GridBagConstraints();
		gbc_lbl.anchor = GridBagConstraints.WEST;
		gbc_lbl.insets = new Insets(3, 0, 5, 5);
		gbc_lbl.gridx = 2;
		gbc_lbl.gridy = 4;
		panelBody.add(lbl, gbc_lbl);
		
		lblDepPhone = new JLabel("Điện Thoại :");
		lblDepPhone.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblDepPhone = new GridBagConstraints();
		gbc_lblDepPhone.anchor = GridBagConstraints.EAST;
		gbc_lblDepPhone.insets = new Insets(3, 5, 5, 5);
		gbc_lblDepPhone.gridx = 0;
		gbc_lblDepPhone.gridy = 5;
		panelBody.add(lblDepPhone, gbc_lblDepPhone);
		
		txtDepPhone = new JTextField();
		txtDepPhone.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtDepPhone.setText(StringUtil.removeUnwantedSpace(txtDepPhone.getText()));
			}
		});
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.fill = GridBagConstraints.BOTH;
		gbc_textField.insets = new Insets(3, 3, 5, 5);
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 5;
		panelBody.add(txtDepPhone, gbc_textField);
		
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(UtilityGUI.colorNote);
		gbc_lbl = new GridBagConstraints();
		gbc_lbl.anchor = GridBagConstraints.WEST;
		gbc_lbl.insets = new Insets(3, 0, 5, 5);
		gbc_lbl.gridx = 2;
		gbc_lbl.gridy = 5;
		panelBody.add(lbl, gbc_lbl);
		
		lblTotalEm = new JLabel("Số Lượng NV :");
		lblTotalEm.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblTotalEm = new GridBagConstraints();
		gbc_lblTotalEm.anchor = GridBagConstraints.EAST;
		gbc_lblTotalEm.insets = new Insets(3, 5, 5, 5);
		gbc_lblTotalEm.gridx = 0;
		gbc_lblTotalEm.gridy = 6;
		panelBody.add(lblTotalEm, gbc_lblTotalEm);
		
		txtTotalEm = new JTextField();
		txtTotalEm.setText("0");
		txtTotalEm.setEditable(false);
		GridBagConstraints gbc_txtTotalEm = new GridBagConstraints();
		gbc_txtTotalEm.anchor = GridBagConstraints.WEST;
		gbc_txtTotalEm.insets = new Insets(3, 3, 5, 5);
		gbc_txtTotalEm.fill = GridBagConstraints.BOTH;
		gbc_txtTotalEm.gridx = 1;
		gbc_txtTotalEm.gridy = 6;
		panelBody.add(txtTotalEm, gbc_txtTotalEm);
				
		panelBtn = new JPanel(new FlowLayout(FlowLayout.CENTER));
		GridBagConstraints gbc_panelBtn = new GridBagConstraints();
		gbc_panelBtn.fill = GridBagConstraints.BOTH;
		gbc_panelBtn.insets = new Insets(15, 10, 10, 10);
		gbc_panelBtn.gridx = 0;
		gbc_panelBtn.gridy = 7;
		gbc_panelBtn.gridwidth = 3;
		panelBody.add(panelBtn, gbc_panelBtn);
		
		btnSave = new JButton("Lưu");
		btnSave.setFont(UtilityGUI.fontBtnForm);
		btnSave.addActionListener(this);
		panelBtn.add(btnSave);
		
		btnCancel = new JButton("Thoát");
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		btnCancel.addActionListener(this);
		panelBtn.add(btnCancel);
		
		groupTxt.add(txtDepName);
		groupTxt.add(txtDepPhone);
		
		lblNote = new JLabel(UtilityGUI.noticeForm);
		lblNote.setFont(UtilityGUI.fontNote);		
		lblNote.setForeground(UtilityGUI.colorNote);
		GridBagConstraints gbc_lblNote = new GridBagConstraints();
		gbc_lblNote.anchor = GridBagConstraints.NORTH;
		gbc_lblNote.insets = new Insets(0, 0, 0, 5);
		gbc_lblNote.gridx = 1;
		gbc_lblNote.gridy = 8;
		panelBody.add(lblNote, gbc_lblNote);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == cbbProvince){
	    	if(cbbProvince.getSelectedIndex()==0){
	    		cbbDistrict.setSelectedIndex(0);
	    		cbbDistrict.setEnabled(false);
	    		return;
	    	}	    	
	    	provinceId = Integer.parseInt(((KeyValue)cbbProvince.getSelectedItem()).getKey()); 
	    	cbbDistrict.setVisible(false);
	    	cbbDistrict = (JComboBox) UtilityGUI.getComboBox("District", provinceId,"");
	    	cbbDistrict.addActionListener(this);
	    	panelBody.add(cbbDistrict, gbc_cbbDistrict);
		}else if(e.getSource() == cbbDistrict){
	    	if(cbbDistrict.getSelectedIndex()==0){
	    		return;
	    	}
	    	districtId = Integer.parseInt(((KeyValue)cbbDistrict.getSelectedItem()).getKey()); 
		}
		if(e.getSource() == btnSave){
			if(!StringUtil.isValidName(txtDepName.getText(), 250, 3)){
				JOptionPane.showMessageDialog(null, 
						UtilityGUI.errorValidName(StringUtil.removeColon(lblDepName.getText()), 250, 3),
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				txtDepName.requestFocusInWindow();
				txtDepName.setBackground(UtilityGUI.txtFocusError);
				return;
		
			}else if(Main.actionDep.checkDepName(txtDepName.getText(), Main.comId) != 0){
				JOptionPane.showMessageDialog(null, 
						"Phòng ban đã tồn tại !\nHãy chọn tên khác...",
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				txtDepName.requestFocusInWindow();
				txtDepName.setBackground(UtilityGUI.txtFocusError);
				return;
				
			}else if(cbbProvince.getSelectedIndex() == 0){
				cbbProvince.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				return;
				
			}else if(cbbDistrict.getSelectedIndex() == 0){
				cbbDistrict.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				return;
				
			}else if(!StringUtil.isValidPhone(txtDepPhone.getText())){
				JOptionPane.showMessageDialog(null, 
						UtilityGUI.errorValid(StringUtil.removeColon(lblDepPhone.getText())),
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				txtDepPhone.requestFocusInWindow();
				txtDepPhone.setBackground(UtilityGUI.txtFocusError);				
				return;
				
			}
			
			Department dep = new Department();
			dep.setDepName(txtDepName.getText());
			dep.setDepDescribe(txaDepDes.getText());
			dep.setDepAddress(txtDepAdd.getText());
			dep.setDepCurrentCity(districtId);
			dep.setDepPhone(txtDepPhone.getText());
			dep.setDepTotalEm(Integer.parseInt(txtTotalEm.getText()));
			dep.setDepCompany(Main.comId);
			if(Main.actionDep.insertDepartment(dep, Main.userId)){
				JOptionPane.showMessageDialog(null, UtilityGUI.successAction,
						UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
				this.setVisible(false);
				Main.jTabbedPane.remove(this);
				int index_ComView = Main.jTabbedPane.positionTab(UtilityGUI.tabComView);
				if(index_ComView > -1){
					Main.jTabbedPane.remove(index_ComView);
					Main.jTabbedPane.addTab(new Company_View(), UtilityGUI.tabComView, index_ComView);
				}
				int index_DepList = Main.jTabbedPane.positionTab(UtilityGUI.tabDepList);
				if(index_DepList == -1){
					Main.jTabbedPane.addTab(new Dep_List(), UtilityGUI.tabDepList);
					Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
				}else{
					Dep_List.tblDepList.setVisible(false);
					int totalPage = Paging.getTotalPage("vi_Department");
					Dep_List.tblDepList = UtilityGUI.getTable("DepartmentCom", totalPage, "Dep_Id", true);
					Dep_List.tblDepList.addMouseListener(new Dep_List.Mouse_tblDep());
					Dep_List.scrollPane.setViewportView(Dep_List.tblDepList);
					Dep_List.cbbPage.setSelectedIndex(totalPage-1);
					Main.jTabbedPane.setSelectedIndex(index_DepList);
				}
			}else{
				JOptionPane.showMessageDialog(null, UtilityGUI.errorAction, 
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				return;
			};
			
		}else if(e.getSource() == btnCancel){
			Main.jTabbedPane.remove(this);
		}
	}

}
